package com.sky.service.user.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.category.DishMapper;
import com.sky.mapper.category.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;

    private final DishMapper dishMapper;

    private final SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO dto) {
        // 先查询当前菜品是否已经存在
        ShoppingCart shoppingCart = shoppingCartMapper.queryBySetmealOrDishId(BaseContext.getCurrentId(), dto.getSetmealId(), dto.getDishId());
        if (shoppingCart != null) {
            log.info("菜品已经存在,直接购物车+1");
            // 如口味为空,则直接+1
            if (!StringUtils.hasText(shoppingCart.getDishFlavor())) {
                shoppingCartMapper.updateNum(shoppingCart.getId(), shoppingCart.getNumber() + 1,null );
                return;
            }
            if (shoppingCart.getDishFlavor().equals(dto.getDishFlavor())) {
                // 口味相同,直接数量加一,口味拼接
                log.info("口味相同,直接数量+1");
                shoppingCartMapper.updateNum(shoppingCart.getId(), shoppingCart.getNumber() + 1, shoppingCart.getDishFlavor());
                return;
            }else {
                //口味不同
                log.info("口味不同,+1同时拼上口味");
                shoppingCartMapper.updateNum(shoppingCart.getId(), shoppingCart.getNumber() + 1, shoppingCart.getDishFlavor()+dto.getDishFlavor());
                return;
            }
        }
        // 若是菜品不存在则新增到购物车
        // 填充公共字段
        shoppingCart = new ShoppingCart();
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishId(dto.getDishId());
        shoppingCart.setSetmealId(dto.getSetmealId());
        DishVO dishVO = dishMapper.queryById(dto.getDishId());
        Setmeal setmeal = setmealMapper.queryById(dto.getSetmealId());
        // 判断接收的参数是否是套餐或者菜品
        if (dto.getSetmealId() != null) {
            // 新增套餐
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCartMapper.insert(shoppingCart);
            return;
        }
        shoppingCart.setName(dishVO.getName());
        shoppingCart.setAmount(dishVO.getPrice());
        shoppingCart.setImage(dishVO.getImage());
        shoppingCart.setDishFlavor(dto.getDishFlavor());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> queryList() {
        return shoppingCartMapper.queryList(BaseContext.getCurrentId());
    }

    @Override
    public void cleanList() {
        shoppingCartMapper.cleanList(BaseContext.getCurrentId());
    }
}
